package module_five.task03;

import module_five.task03.ClientModel.*;

import java.io.IOException;

/**
 * 编程实现客户端主界面的绘制和相应功能的实现
 */
public class ClientView {

    /**
     * 合成复用原则, 引入 ClientInitClose对象, 获取对象输出流和对象输入流
     */
    private ClientInitClose cic;

    /**
     * 合成复用原则, 引入StudentManager对象, 实现对学员的增删改查等操作
     */
    private ClientStudentManager sm;

    /**
     * 通过构造方法实现成员变量初始化
     * @param cic
     */
    public ClientView(ClientInitClose cic, ClientStudentManager sm) {
        this.cic = cic;
        this.sm = sm;
    }


    /**
     * 用户操作框
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void clientMainPage() throws IOException, ClassNotFoundException {

        while (true) {
            System.out.println("  \n\n\t\t在线考试系统");
            System.out.println("------------------------------");

            System.out.print("   [1] 学员系统");
            System.out.println("    [2] 管理员系统");
            System.out.println("    [0] 退出系统");

            System.out.println("------------------------------");

            System.out.println("请选择要进行的业务编号:");

            int choose = ClientScanner.getScanner().nextInt();

            switch (choose) {
                case 1:
                    System.out.println("正在进入学员系统...");
                    break;
                case 2:
                    clientManagerLogin();
                    System.out.println("正在进入管理员系统...");
                    clientStudentModelPage();
                    break;
                case 0:
                    System.out.println("正在退出系统");
                    return;
                default:
                    System.out.println("输入错误, 请重新选择!");
            }
        }
    }

    /**
     * 管理员系统操作页面
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void clientStudentModelPage() throws IOException, ClassNotFoundException {

        while (true) {
            System.out.println("  \n\n\t\t学员管理模块");
            System.out.println("------------------------------");

            System.out.print("     [1] 添加学员");
            System.out.println("    [2] 修改学员");
            System.out.print("    [3] 查询学员");
            System.out.println("    [4] 删除学员");
            System.out.println("    [5] 退出学员管理系统");

            System.out.println("请选择要进行的业务编号:");

            int choose = ClientScanner.getScanner().nextInt();

            switch (choose) {
                case 1:
                    System.out.println("添加学员");
                    managerAddStudent();
                    break;
                case 2:
                    System.out.println("修改学员");
                    manageModifyStudent();
                    break;
                case 3:
                    System.out.println("查询学员");
                    managerSelectStudent();
                    break;
                case 4:
                    System.out.println("删除学员");
                    manageDeleteStudent();
                    break;
                case 5:
                    System.out.println("退出系统");
                    clientMainPage();
                    break;
                default:
                    System.out.println("输入错误, 请重新选择");
                    break;
            }
        }

    }

    /**
     * 修改学员信息
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void manageModifyStudent() throws IOException, ClassNotFoundException {

        Student student = sm.modifyStudent();

        StudentMessage studentMessage = new StudentMessage("modify", student);

        cic.getOos().writeObject(studentMessage);
        System.out.println("客户端发送修改学员信息成功!");

        studentMessage = (StudentMessage) cic.getOis().readObject();
        if ("success".equals(studentMessage.getType())) {
            System.out.println("修改学员信息成功!");
        } else {
            System.out.println("修改学员信息失败! 要修改的学员不存在!");
        }
    }



    /**
     * 自定义成员方式, 实现管理员登录的功能
     */
    private void clientManagerLogin() throws IOException, ClassNotFoundException {

        System.out.println("请输入管理员的姓名:");
        String userName = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码:");
        String password = ClientScanner.getScanner().next();

        UserMessage um = new UserMessage("managerCheck", new User(userName, password));

        cic.getOos().writeObject(um);
        System.out.println("客户端发送管理员账户信息成功!");

        um = (UserMessage) cic.getOis().readObject();
        if ("success".equals(um.getType())) {
            System.out.println("登录成功, 欢迎使用!");
        } else {
            System.out.println("账号或密码错误!");
        }
    }

    /**
     * 添加一个学员
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void managerAddStudent() throws IOException, ClassNotFoundException {

        Student student = sm.addStudent();

        StudentMessage studentMessage = new StudentMessage("add", student);

        cic.getOos().writeObject(studentMessage);
        System.out.println("客户端发送添加学员信息成功!");

        studentMessage = (StudentMessage) cic.getOis().readObject();
        if ("success".equals(studentMessage.getType())) {
            System.out.println("添加学员成功!");
        } else {
            System.out.println("添加学员失败, 不可重复添加相同学号的学员!");
        }
    }

    /**
     * 查找一个学员
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void managerSelectStudent() throws IOException, ClassNotFoundException {

        Student student = sm.selectStudent();

        StudentMessage studentMessage = new StudentMessage("select", student);

        cic.getOos().writeObject(studentMessage);
        System.out.println("客户端发送查询学员信息成功!");

        studentMessage = (StudentMessage) cic.getOis().readObject();
        if ("success".equals(studentMessage.getType())) {
            System.out.println("查询学员信息成功!学员具体信息为:" + studentMessage.getStudent());
        } else {
            System.out.println("不存在该学员!");
        }
    }

    /**
     * 删除学员
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void manageDeleteStudent() throws IOException, ClassNotFoundException {

        Student student = sm.deleteStudent();

        StudentMessage studentMessage = new StudentMessage("delete", student);

        cic.getOos().writeObject(studentMessage);
        System.out.println("客户端发送删除学员信息成功!");

        studentMessage = (StudentMessage) cic.getOis().readObject();
        if ("success".equals(studentMessage.getType())) {
            System.out.println("删除学员信息成功!");
        } else {
            System.out.println("删除学员信息失败! 不存在该学员!");
        }
    }
}
